import urllib2
import httplib
import re

class UrlHelper:
    @staticmethod
    def get_content(url):

        try:
            req = urllib2.Request(url)
            response = urllib2.urlopen(req)
            return response.read()
        except httplib.BadStatusLine:
            print 'error', url
            raise Exception("Error connecting to " + url)

    @staticmethod
    def get_seasons(idSerie):
        html = UrlHelper.get_content('http://www.cuevana.tv/list_search_id.php?serie=' + str(idSerie))
        return html
        
    @staticmethod
    def get_chapters(idSeason):
        html = UrlHelper.get_content('http://www.cuevana.tv/list_search_id.php?temporada=' + str(idSeason))
        return html

    @staticmethod
    def get_serie_data(idSerie):
        html = UrlHelper.get_content('http://www.cuevana.tv/list_search_info.php?serie=' + str(idSerie))
        return html

    @staticmethod
    def get_season_data(idSeason):
        html = UrlHelper.get_content('http://www.cuevana.tv/list_search_info.php?temporada=' + str(idSeason))
        return html

    @staticmethod
    def get_full_series_list():
        html = UrlHelper.get_content('http://www.cuevana.tv/series/')
        matches = re.findall('id:(\d+),nombre:"(.*)"', str(html))
        matches = sorted(matches,key=lambda s: s[1])
        return matches
